package com.wang.mapreduce;

import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;

import java.io.IOException;

/**
 * @Author: wang
 * @Date: 2020/1/26 18:13
 * @Description:
 */
public class FruitReducer extends TableReducer<LongWritable, Text, NullWritable> {

    @Override
    protected void reduce(LongWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        // 遍历 values：1001   Apple   Red
        for (Text value : values) {
            // 获取每行数据
            String[] fields = value.toString().split("\t");
            // 构建 put 对象
            Put put = new Put(Bytes.toBytes(fields[0]));
            // 给 put 对象赋值
            put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"),
                    Bytes.toBytes(fields[1]));
            put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("color"),
                    Bytes.toBytes(fields[2]));
            context.write(NullWritable.get(), put);
        }
    }
}
